<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Organization extends SMB_Controller {
	
	public function __construct() {
		parent::__construct();
		$this->load->library('data');
	}
	
	public function index() {
		$this->_get_userinfo();
		$this->load->helper('table');
		$limit = $this->data->count_organization($this->userinfo['username']);
			
		$this->_load_common_header();
		$this->_load_common_navigation();
		$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'profile'));
		$params = array(
			'organizations' => $this->data->get_organization($this->userinfo['username'], '0', $limit),
			'addstatus' => $this->session->flashdata('addstatus'),
			'editstatus' => $this->session->flashdata('editstatus'),
			'deletestatus' => $this->session->flashdata('deletestatus'),
			'pager' => get_pager(4)
		);
		$this->load->view('mahasiswa/organization_list', $params);
		$this->_load_common_footer();
	}
	
	public function add() {
		$this->_get_userinfo();
		
		$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
		$status = $this->input->post('save');
		if ($this->form_validation->run('add_organization') == FALSE) {
			if ($status == 'Batal') {
				redirect('mahasiswa/profile/organization', 'location');
			} else {
				$this->_load_common_header();
				$this->_load_common_navigation();
				$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'profile'));
				$params = array(
					'name' => $this->session->flashdata('name'),
					'year' => $this->session->flashdata('year'),
					'position' => $this->session->flashdata('position'),
					'addstatus' => $this->session->flashdata('addstatus')
				);
				$this->load->view('mahasiswa/organization_form', $params);
				$this->_load_common_footer();
			}
		} else {
			if ($status == 'Simpan') {
				$name = $this->input->post('name');
				$year = $this->input->post('year');
				$position = $this->input->post('position');
				$result = $this->data->add_organization($this->userinfo['username'], $name, $year, $position);
				if ($result == FALSE) {
					$this->session->set_flashdata('name', $name);
					$this->session->set_flashdata('year', $year);
					$this->session->set_flashdata('position', $position);
					$this->session->set_flashdata('addstatus', 'failed');
					redirect('mahasiswa/profile/organization/add', 'refresh');
				} else {
					$this->session->set_flashdata('addstatus', 'success');
					redirect('mahasiswa/profile/organization', 'refresh');
				}
			} else {
				redirect('mahasiswa/profile/organization', 'refresh');
			}
		}
	}
	
	public function _current_year ($str) {
		$current_year = intval(date('Y'));
		$str = intval($str);
		
		if($str > $current_year){
			$this->form_validation->set_message('_current_year', 'Tahun organisasi tidak boleh lebih dari tahun sekarang.');
			return FALSE;
		} else{
			return TRUE;		
		}
		
	}
	
	public function edit($id = NULL) {
		if ($id == NULL) {
			redirect('mahasiswa/profile/organization', 'location');
		} else {
			$this->_get_userinfo();
			$organization = $this->data->get_organization_by_id($id);
			if ($organization == NULL) {
				redirect('mahasiswa/profile/organization', 'refresh');
				exit;
			}
			if ($organization->username != $this->userinfo['username']) {
				redirect('mahasiswa/profile/organization', 'refresh');
				exit;
			}
			
			$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
			$status = $this->input->post('save');
			if ($this->form_validation->run('add_organization') == FALSE) {
				if ($status == 'Batal') {
					redirect('mahasiswa/profile/organization', 'location');
				} else {
					$this->_load_common_header();
					$this->_load_common_navigation();
					$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'profile'));
					$params = array(
						'organization' => $organization,
						'editstatus' => $this->session->flashdata('editstatus'),
						'name' => $this->session->flashdata('name'),
						'year' => $this->session->flashdata('year'),
						'position' => $this->session->flashdata('position')
					);
					$this->load->view('mahasiswa/organization_editform', $params);
					$this->_load_common_footer();
				}
			} else {
				if ($status == 'Simpan') {
					$name = $this->input->post('name');
					$year = $this->input->post('year');
					$position = $this->input->post('position');
					$result = $this->data->edit_organization($id, $name, $year, $position);
					if ($result == FALSE) {
						$this->session->set_flashdata('name', $name);
						$this->session->set_flashdata('year', $year);
						$this->session->set_flashdata('position', $position);
						$this->session->set_flashdata('editstatus', 'failed');
						redirect('mahasiswa/profile/organization/edit/'.$id, 'refresh');
					} else {
						$this->session->set_flashdata('editstatus', 'success');
						redirect('mahasiswa/profile/organization', 'refresh');
					}
				} else {
					redirect('mahasiswa/profile/organization', 'refresh');
				}
			}
		}
	}
	
	public function delete() {
		if ($this->input->is_ajax_request()) {
			$id = $this->input->post('id');
			if ($id != FALSE) {
				$result = $this->data->delete_organization($id);
				if ($result == TRUE) {
					echo 'Organisasi berhasil dihapus.';
					exit;
				} else {
					echo 'Gagal hapus organisasi. Silakan coba beberapa saat lagi.';
					exit;	
				}
			} else {
				echo 'Gagal hapus organisasi. Silakan coba beberapa saat lagi.';
				exit;
			}
		} else {
			redirect('mahasiswa/profile/organization/index', 'location');
			exit;
		}
	}
}